/**
 * Created by wuye on 2017/12/11.
 */
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/book/rewardList',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'tdBook.id', index: "tdBook.id", hidden:true,key:true},
            { label: '书籍名称', name: 'tdBook.bookname', width: 75 },
            { label: '分类', name: 'tdBook.categoryname', width: 90 },
            { label: '最新章节', name: 'tdBook.newsection', width: 80 },
            { label: '打赏量', name: 'tdBook.rewardCount', width: 80 },
            { label: '日收入(蝉币)', name: 'rewardDay', width: 90,formatter: function(value, options, row) {
                    if (value) {
                        return value/100
                }else {
                        return 0
                    }
            }
            },
            { label: '月收入(蝉币)', name: 'rewardMonth', width: 90,formatter: function(value, options, row) {
                    if (value) {
                        return value / 100
                    }else {
                        return 0
                    }
                }},
            { label: '总收入(蝉币)', name: 'tdBook.rewardMoney', width: 90,formatter: function(value, options, row) {
                    if (value) {
                        return value / 100
                    }else {
                        return 0
                    }
                }},

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
    });
    $("#jqGrid2").jqGrid({
        url: baseURL + 'sys/book/reward/',
        datatype: "json",
        colModel: [
            // { label: 'ID', name: 'id', index: "id", width: 45, key: true },
            { label: '书籍名称', name: 'bookName', width: 75 },
            { label: '打赏金额（蝉币）', name: 'rewardMoney', width: 90 ,formatter: function(value, options, row){
                    return value /100 }},
            { label: '打赏日期', name: 'rewardDate', width: 300 },
        ],
        viewrecords: true,
        width:'100%',
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager2",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order",
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid2").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("table[role='grid']").each(function () {//jqgrid 创建的表格都有role属性为grid
                $('.' + $(this).attr("class") + ' tr:first th:first').css("width", "45"); //使表头的序号列宽度为40
                $('.' + $(this).attr("class") + ' tr:first td:first').css("width", "45"); // 使表体的序号列宽度为40
            });
        }
    });
});



var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            bookname: null
        },
        showList: true,
        title:null,
        // roleList:{},
        banner:{
            bannerpic:null
        },
        sels:[

        ],
        checked:false,
    },
    created:function () {
        $.ajax({
            url: baseURL + 'sys/book/findAll',
            type: "POST",
            contentType: "application/json",
            success: function(result) {
                vm.sels = result.list
            },
            error: function(error) {
                alert('数据加载失败!');
            }
        })
    },
    methods: {
        check:function(){
            var id=getSelectedRow();
            if(id){
                console.log(getSelectedRow())
                vm.getComments(id);
                vm.checked=true;
                $("#jqGrid2").setGridWidth(document.body.clientWidth*0.99);
            }else {
                return;
            }
        },
        // clickMe:function (id) {
        //     alert(212121)
        // },
        getComments:function(id){
            var page = $("#jqGrid2").jqGrid('getGridParam','page');
            $("#jqGrid2").jqGrid('setGridParam',{
                page:page,
                postData:{
                    bookId:id,
                },
            }).trigger("reloadGrid");
        },
        changeImage: function(e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function(e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        upload: function(data) {
            // 上传图片
            var THIS = this;
            if(this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function(result) {
                        THIS.banner.bannerpic = result.url
                    },
                    error: function(error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.banner.bannerpic=null;
            vm.banner.bookname=null;
            vm.banner.id=null;
        },
        query: function () {
            vm.reload();
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getBanner(id);

        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/banner/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        update2: function () {
            var url = "sys/banner/update2";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.category),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getBanner: function(id){
            $.get(baseURL + "sys/banner/findById/"+id, function(r){
                vm.banner = r.banner;
            });
        },
        saveOrUpdate: function () {
            var url = "sys/banner/saveOrUpdate/" //vm.user.userId == null ? "sys/user/save" : "sys/user/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.banner),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.checked = false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'bookname': vm.q.bookname},
                page:page
            }).trigger("reloadGrid");
            console.log(page)
        }
    }
});
